<template>
  <div class="goods-sku" v-if="goods">
    <dl v-for="item in goods.specs" :key="item.id">
      <dt>{{item.name}}</dt>
      <dd>
        <template v-for="sub in item.values" :key="sub.id">
          <img :class="{selected: sub.state,disabled:sub.disabled}" @click="clickSpecs(item,sub)" v-if="sub.picture" :src="sub.picture" :title="sub.name">
          <span :class="{selected: sub.state,disabled:sub.disabled}" @click="clickSpecs(item,sub)" v-else>{{sub.name}}</span>
        </template>
      </dd>
    </dl>
  </div>
</template>
<script>
import getPowerSet from '@/vender/power-set'
const spliter = '★'
// 获取路径字典
const getPathMap = (skus) => {
  const pathMap = {}
  // 判断出有库存的
  skus.forEach(sku => {
    if (sku.inventory > 0) {
      const specs = sku.specs.map(val => val.valueName)
      const powerSet = getPowerSet(specs)
      powerSet.forEach(set => {
        const key = set.join(spliter)
        if (pathMap[key]) {
          pathMap[key].push(sku.id)
        } else {
          pathMap[key] = [sku.id]
        }
      })
    }
  })
  return pathMap
}
const getSelectdValues = (specs) => {
  const arr = []
  specs.forEach(item => {
    // 选中的按钮对象
    const selectedVal = item.values.find(val => val.state)
    arr.push(selectedVal ? selectedVal.name : undefined)
  })
  return arr
}
// 更新按钮禁用状态
const updateDisabledStatus = (specs, pathMap) => {
  specs.forEach((item, index) => {
    const selectdValues = getSelectdValues(specs)
    item.values.forEach(val => {
      // 去路径字典里面查找是否有数据，有可以点击，没有就禁用
      // 判断当前是否选中
      if (val.state) return
      // 拿当前的值按照顺序套入选中的值数组
      selectdValues[index] = val.name
      // 提出undefined
      const key = selectdValues.filter(value => value).join(spliter)
      val.disabled = !pathMap[key]
    })
  })
}

// 默认选中的方法
const initSelectedStatus = (goods, skuId) => {
  const sku = goods.skus.find(sku => sku.id === skuId)
  goods.specs.forEach((item, i) => {
    const val = item.values.find(val => val.name === sku.specs[i].valueName)
    val.state = true
  })
}
// const
export default {
  name: 'GoodsSku',
  props: {
    goods: {
      type: Object,
      default: () => ({})
    },
    skuId: {
      type: String,
      default: ''
    }
  },
  setup (props, { emit }) {
    // 组件初始化得到路径字典
    const pathMap = getPathMap(props.goods.skus)
    // 组件初始化更新按钮的状态
    if (props.skuId) {
      initSelectedStatus(props.goods, props.skuId)
    }
    updateDisabledStatus(props.goods.specs, pathMap)
    // 点击按钮更改状态
    const clickSpecs = (item, sub) => {
      if (sub.disabled) return
      if (sub.state) {
        return
      } else {
        item.values.forEach(i => { i.state = false })
        sub.state = true
      }
      // 每次点击按钮是更新状态
      updateDisabledStatus(props.goods.specs, pathMap)
      // 选中后的sku通知父组件{skuid price oldprice inventory specsText}
      const selectedValues = getSelectdValues(props.goods.specs).filter(v => v)
      // 按钮每一项都有选中触发
      if (selectedValues.length === props.goods.specs.length) {
        const skuIds = pathMap[selectedValues.join(spliter)]
        const sku = props.goods.skus.find(item => item.id === skuIds[0])
        emit('change', {
          inventory: sku.inventory,
          skuId: sku.id,
          price: sku.price,
          oldPrice: sku.oldPrice,
          specsText: sku.specs.reduce((p, c) => `${p} ${c.name}:${c.valueName}`, '').trim()
        })
      } else {
        // 不完整传递空对象
        emit('change', {})
      }
    }
    return { clickSpecs }
  }
}

</script>
<style scoped lang="less">
.sku-state-mixin () {
  border: 1px solid #e4e4e4;
  margin: 0 10px 10px 0;
  cursor: pointer;
  &.selected {
    border-color: @xtxColor;
  }
  &.disabled {
    opacity: 0.6;
    border-style: dashed;
    cursor: not-allowed;
  }
}
.goods-sku {
  padding-left: 10px;
  padding-top: 20px;
  dl {
    display: flex;
    padding-bottom: 20px;
    align-items: center;
    dt {
      width: 50px;
      color: #999;
    }
    dd {
      flex: 1;
      color: #666;

      > img {
        width: 50px;
        height: 50px;
        .sku-state-mixin ();
      }
      > span {
        display: inline-block;
        height: 30px;
        line-height: 28px;
        padding: 0 20px;
        .sku-state-mixin ();
      }
    }
  }
}
</style>
